package com.store.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.R;
import com.store.app.activity.CashierActivity;
import com.store.app.bean.GoodsListBean;


/**
 * Created by Administrator on 2016/3/1.
 */
public class KeyboardFragment extends Fragment{
    private View v;                             //布局
    private TextView pay_one,pay_two,pay_three, //数字键盘
                     pay_four,pay_five,pay_six,
                     pay_seven,pay_eight,pay_nine,
                     pay_zero,pay_space,pay_del;
    private Button btOk;                       //点击确认添加数字键盘添加
    private String moneytext;
    private TextView pay_money;                //显示数字键盘金额
    private TextView pay_affirm,pay_cancel,reminder;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_keyboard, container, false);
        initView();
        return v;
    }
    public void initView(){
        //显示输入金额文本
        pay_money = (TextView) v.findViewById(R.id.tv_keyboard_text);

        btOk = (Button) v.findViewById(R.id.pay_affirm);
        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //往商品列表添加一笔数据








                if(TextUtils.isEmpty(pay_money.getText().toString().trim())){
                    return;
                }else{
                    double moneyDouble = Double.parseDouble(pay_money.getText().toString().trim());
                    String moneyHead2="";
                    if(pay_money.getText().toString().trim().length()>1){
                        moneyHead2 = pay_money.getText().toString().trim().substring(0,2);//如果前两位开头是00则提示无效
                        //如果前两位不是0.开头 那说明数据无效
                        if(moneyHead2.substring(0,1).equals("0")){
                            if(!moneyHead2.equals("0.")){
                                Toast.makeText(getActivity(),"输入金额无效",
                                        Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }
                    }

                    Log.v("zyl","moneyHead2:"+moneyHead2);
                    if(moneyHead2.equals("00")){
                        Toast.makeText(getActivity(),"输入金额无效",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }


                    if(moneyDouble<=0){
                        Toast.makeText(getActivity(), "输入金额无效",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if(Double.parseDouble(pay_money.getText().toString().trim())>2000){
                    Toast.makeText(getActivity(),"金额超过2000,请重新输入！",Toast.LENGTH_SHORT).show();
                    pay_money.setText("");
                    return;
                }

                GoodsListBean bean = new GoodsListBean();
                bean.setCount(1);
                bean.setPrice(moneytext+"");
                bean.setGoodsName("");
                CashierActivity.goodsListBeanList.add(bean);
                CashierActivity.handler.sendEmptyMessage(1);
                pay_money.setText("");
            }
        });




        pay_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                Log.v("zyl", "文本内容变化start:" + start);
//                Log.v("zyl", "文本内容变化before:" + before);
//                Log.v("zyl", "文本内容变化CharSequence:" + s);
//                Log.v("zyl", "文本内容变化count:" + count);
                if (TextUtils.isEmpty(s)) {
                    btOk.setBackgroundResource(R.drawable.rect_button_ok_normal);
                } else {
                    btOk.setBackgroundResource(R.drawable.rect_button_ok_press);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        pay_one = (TextView)v.findViewById(R.id.pay_one);
        pay_one.setOnClickListener(myListener);

        pay_two = (TextView)v.findViewById(R.id.pay_two);
        pay_two.setOnClickListener(myListener);

        pay_three = (TextView)v.findViewById(R.id.pay_three);
        pay_three.setOnClickListener(myListener);

        pay_four = (TextView)v.findViewById(R.id.pay_four);
        pay_four.setOnClickListener(myListener);

        pay_five = (TextView)v.findViewById(R.id.pay_five);
        pay_five.setOnClickListener(myListener);

        pay_six = (TextView)v.findViewById(R.id.pay_six);
        pay_six.setOnClickListener(myListener);

        pay_seven = (TextView)v.findViewById(R.id.pay_seven);
        pay_seven.setOnClickListener(myListener);

        pay_eight = (TextView)v.findViewById(R.id.pay_eight);
        pay_eight.setOnClickListener(myListener);

        pay_nine = (TextView)v.findViewById(R.id.pay_nine);
        pay_nine.setOnClickListener(myListener);

        pay_zero = (TextView)v.findViewById(R.id.pay_zero);
        pay_zero.setOnClickListener(myListener);
        pay_space = (TextView)v.findViewById(R.id.pay_space);
        pay_space.setOnClickListener(myListener);

        pay_del = (TextView)v.findViewById(R.id.pay_del);
        pay_del.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                moneytext = pay_money.getText().toString();
                int index = moneytext.length();
                if(index!=0){
                    moneytext=moneytext.substring(0,moneytext.length()-1);
                    pay_money.setText(moneytext);
                }
            }
        });
    }

    View.OnClickListener myListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            moneytext=pay_money.getText().toString();
            Log.v("zyl","moneytext:"+moneytext);
            if(moneytext.contains(".")){
                String[] tempS = moneytext.split("\\.");
                if(tempS.length>1){
                    int resultNum = tempS[1].length();
                    if(resultNum>1){
                        return;
                    }}
            }

            switch (v.getId()) {
                case R.id.pay_space:
                    moneytext=pay_money.getText().toString();
                    Log.v("zyl","moneytext:"+moneytext);
                    if(!moneytext.contains(".")){
                        if(moneytext.equals("")){
                            input_digit("0.");
                        }else{
                            input_digit(".");
                        }
                    }
                    break;

                case R.id.pay_zero:
                    input_digit("0");

                    break;
                case R.id.pay_one:
                    input_digit("1");

                    break;
                case R.id.pay_two:
                    input_digit("2");

                    break;
                case R.id.pay_three:
                    input_digit("3");

                    break;
                case R.id.pay_four:
                    input_digit("4");

                    break;
                case R.id.pay_five:
                    input_digit("5");

                    break;
                case R.id.pay_six:
                    input_digit("6");

                    break;
                case R.id.pay_seven:
                    input_digit("7");

                    break;
                case R.id.pay_eight:
                    input_digit("8");

                    break;
                case R.id.pay_nine:
                    input_digit("9");

                    break;
                default:
                    break;
            }
        }
    };

    private void input_digit(String digit) {
        moneytext = pay_money.getText().toString();
        if(moneytext.indexOf(".") != -1){
            //获取小数点的位置
            int num = 0;
            num = moneytext.indexOf(".");
            if(moneytext.length()-num>2){

            }else{
                moneytext =  moneytext+digit;
            }
        }else{
            moneytext =  moneytext+digit;
        }
        pay_money.setText(moneytext);
    }
}
